import { checkLoginReq } from '@/api/login'

//解构ajax接口函数

const state = {
    token: localStorage.getItem('t_k'),
    role: localStorage.getItem('role'),
}

const mutations = {
    //获取token
    SET_TOKEN(state, data) {
        //储存到vuex里
        state.token = data
            //储存到本地
        localStorage.setItem('t_k', data)
    },
    //获取角色
    SET_ROLE(state, data) {
        state.role = data
        localStorage.setItem('role', data)
    },
}
const actions = {
    //登录
    A_login({ commit }, data) {
        return new Promise(async resolve => {
            let res = await checkLoginReq(data)
            let { code, role, token } = res.data
            if (code === 0) {
                commit('SET_TOKEN', 'Bearer ' + token)
                commit('SET_ROLE', role)
                resolve()
            }
        })
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions,
}